//
//  HospMainViewController.m
//  Doctor1.0
//
//  Created by mk on 15/11/19.
//  Copyright (c) 2015年 cocoa-mk. All rights reserved.
//

#import "HospMainViewController.h"
#import "Hospital.h"
#import "HosptTableViewCell.h"
#import "CityOrDepartmentrCell.h"
#import "HosDepartmentViewController.h"

@interface HospMainViewController ()
{
    NSMutableArray * arrayCity1;
    NSMutableArray * arrDep;
    HospitalList * hosList;
    NSInteger selectCity;
    NSInteger selectSubCity;//子城市
    NSString * strCityID;
    NSString * strKeshiID;
}

@end

@implementation HospMainViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[BaiduMobStat defaultStat] pageviewStartWithName:@"HospMainViewController"];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [[BaiduMobStat defaultStat] pageviewEndWithName:@"HospMainViewController"];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self hideAllView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (NSVersionNumber_iOS_7_0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        UIView * viewNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, -64, appScreenWidth, 64)];
        viewNav.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:viewNav];
    }
    UILabel * titleNav = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 30)];
    titleNav.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18];
    titleNav.backgroundColor = [UIColor clearColor];
    titleNav.textAlignment = NSTextAlignmentCenter;
    titleNav.textColor = defaultNavBackColor;
    [self.navigationItem setTitleView:titleNav];
    titleNav.text = @"推荐医院";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    arrayCity1 = [[NSMutableArray alloc]initWithCapacity:0];
    arrDep = [[NSMutableArray alloc]initWithObjects:@"全部",@"外科",@"骨科",@"妇产科",@"眼科",@"口腔科",@"小儿外科",@"其他", nil];
    
    hosList = [[HospitalList alloc]init];
    hosList.needLoadMore = YES;
    strCityID = @"1";
    strKeshiID = @"0";
    selectSubCity = 0;
    selectCity = 0;
    [self getCity];
    [tableMain becomeRefreshing:YES];
}


#pragma mark 获取数据
-(void)getCity
{
    NSString * strUrl = [NSString stringWithFormat:@"/city"];
    [[HttpClient manager]GET:strUrl
                  parameters:nil
                     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         id data = [responseObject objectForKey:requestDataKey];
         if ([data isKindOfClass:[NSArray class]])
         {
             arrayCity1 = data;
             if (arrayCity1.count > 0)
             {
                 NSMutableDictionary * dic = [arrayCity1 objectAtIndex:0];
                 NSMutableArray * arr = [dic objectForKey:@"subCity"];
                 NSMutableDictionary * dicSub = [arr objectAtIndex:0];
                 NSString * strCity = [NSString stringWithFormat:@"%@",[dicSub objectForKey:@"city"]];
                 
                 strCityID = [NSString stringWithFormat:@"%@",[dicSub objectForKey:@"id"]];
                 labCity.text = strCity;
                 [tableCity1 reloadData];
                 [tableCity2 reloadData];
             }
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         HUDShowRequestError(error.domain, self.view)
     }];
    
}


- (void)getTeamData
{
    if ([strKeshiID isEqualToString:@"0"])
    {
        hosList.urlPath = [NSString stringWithFormat:@"/appnav3?city=%@",strCityID];
    }else
    {
        hosList.urlPath = [NSString stringWithFormat:@"/appnav3?city=%@&cate=%@",strCityID,strKeshiID];
    }
    hosList.strDataKey = @"hospitals";
    [hosList loadPageData:^(AFHTTPRequestOperation *operation, id responseObject, BOOL haveNextPage)
     {
        // NSLog(@"==%@",responseObject);
         [tableMain reloadData];
         [tableMain refreshFinished];
         tableMain.loadMoreView.state = (haveNextPage?LOADMORENORMAL:LOADMOREFINISHED);
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [tableMain refreshFinished];
         tableMain.loadMoreView.state = LOADMORENORMAL;
         HUDShowRequestError(error.domain, self.view)
     }];
}


-(IBAction)selectCity:(id)sender
{
    if (!viewKeshi.hidden)
    {
        viewKeshi.hidden = YES;
    }
    [UIView animateWithDuration:0.1 animations:^
     {
         viewCity.hidden = !viewCity.hidden;
     }];
    
    if(arrayCity1.count == 0)
    {
     [self getCity];
    }
}

-(IBAction)selectKeshi:(id)sender
{
    if (!viewCity.hidden)
    {
        viewCity.hidden = YES;
    }
    [UIView animateWithDuration:0.1 animations:^
     {
         viewKeshi.hidden = !viewKeshi.hidden;
     }];
}


-(IBAction)hideAllView
{
    [UIView animateWithDuration:0.1 animations:^{
        viewKeshi.hidden = YES;
        viewCity.hidden = YES;
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == tableMain)
    {
        return hosList.dataArray.count;
    }
    else if (tableView == tableKeshi)
    {
        return arrDep.count;
    }else if (tableView == tableCity1)
    {
        return arrayCity1.count;
        
    }else if (tableView == tableCity2)
    {
        if(arrayCity1.count>0)
        {
            NSMutableDictionary * dic = [arrayCity1 objectAtIndex:selectCity];
            return [[dic objectForKey:@"subCity"] count];
        }
    }
    
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == tableMain)
    {
        return 75;
    }
    return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == tableMain)
    {
        static NSString * strCell = @"HosptTableViewCellStr";
        
        HosptTableViewCell *cell = (HosptTableViewCell *)[tableView dequeueReusableCellWithIdentifier:strCell];
        if (!cell)
        {
            cell = (HosptTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"HosptTableViewCell" owner:self options:nil] objectAtIndex:0];
        }
        
        [cell fillDataWith:[hosList.dataArray objectAtIndex:indexPath.row]];
        return cell;
    }else
    {
        static NSString *CellTableIdentifier = @"CityOrDepartmentrCellStr";
        
        CityOrDepartmentrCell *cell = (CityOrDepartmentrCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
        if (!cell)
        {
            cell = (CityOrDepartmentrCell *)[[[NSBundle mainBundle] loadNibNamed:@"CityOrDepartmentrCell" owner:self options:nil] objectAtIndex:0];
        }
        cell.backgroundColor = tableView.backgroundColor;
        if (tableView == tableKeshi)
        {
            [cell fillTitleDataWith:[arrDep objectAtIndex:indexPath.row] showLine:YES];
           // [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        }
        if (tableView == tableCity1)
        {
            [cell fillTitleDataWith:[[arrayCity1 objectAtIndex:indexPath.row] objectForKey:@"state"] showLine:NO];
           // [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectCity inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        }
        if (tableView == tableCity2)
        {
        //    [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectSubCity inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
//            if ([tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
       //     [tableView.delegate tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:selectCity inSection:0]];
            
            NSMutableDictionary * dic = [arrayCity1 objectAtIndex:selectCity];
            NSMutableArray * arrSub = [dic objectForKey:@"subCity"];
            NSString * strCity = [[arrSub objectAtIndex:indexPath.row] objectForKey:@"city"];
            [cell fillTitleDataWith:strCity showLine:YES];
        }
        
        return cell;
    }
    return nil;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == tableMain)
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        HosDepartmentViewController * dep = [[HosDepartmentViewController alloc]initWithNibName:@"HosDepartmentViewController" bundle:nil];
        dep.hosInfo = [hosList.dataArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:dep animated:YES];
    }
    else if (tableView == tableKeshi)
    {
        strKeshiID = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        labKeshi.text = [arrDep objectAtIndex:indexPath.row];
        [self hideAllView];
        [tableMain becomeRefreshing:YES];
        
    }else if (tableView == tableCity1)
    {
        selectCity = indexPath.row;
        [tableCity2 reloadData];
        
    }else if (tableView == tableCity2)
    {
        selectSubCity = indexPath.row;
        NSMutableDictionary * dic = [arrayCity1 objectAtIndex:selectCity];
        NSMutableArray * arrSub = [dic objectForKey:@"subCity"];
        NSString * strCity = [[arrSub objectAtIndex:indexPath.row] objectForKey:@"city"];
        strCityID = [[arrSub objectAtIndex:indexPath.row] objectForKey:@"id"];
        labCity.text = strCity;
        [self hideAllView];
        [tableMain becomeRefreshing:YES];
    }
}


#pragma mark pullDelegate
#pragma -mark delegates
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    [tableMain setCanLoadMore:YES];
    hosList.requestPage = kLoadMoreStartPage;
    hosList.strLastId = @"1";
    [self getTeamData];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView*)pullTableView
{
    [tableMain refreshFinished];
    [self getTeamData];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
